﻿using System;
using System.Collections.Generic;
using System.Text;
using moc.IBLL;
using moc.Models;
using moc.IDAL;
using moc.Common;
using System.Linq;

namespace moc.BLL
{
    public partial class CustomerInfoBll :ICustomerInfoBll
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly ICustomerInfoDal _infoDal;
        private readonly ICustomerLoginDal _loginDal;
        public CustomerInfoBll(IUnitOfWork unitOfWork,ICustomerInfoDal infoDal,ICustomerLoginDal loginDal)
        {
            _unitOfWork = unitOfWork;
            _infoDal = infoDal;
            _loginDal = loginDal;
        }

        /// <summary>
        /// 添加新用户
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool AddCustomer(string loginName, string password, string email)
        {
            CustomerLogin login = new CustomerLogin()
            {
                Login_name = loginName,
                Password = password
            };
            _loginDal.Add(login);
            CustomerInfo info = new CustomerInfo()
            {
                Customer_id = login.Customer_id,
                Customer_email = email,
                Register_time = DateTime.Now
            };
            _infoDal.Add(info);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="infoList"></param>
        /// <param name="loginList"></param>
        /// <returns></returns>
        public bool DeleteCustomer(List<CustomerInfo> infoList,List<CustomerLogin> loginList)
        {
            _loginDal.Delete(loginList);
            _infoDal.Delete(infoList);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="cust"></param>
        /// <returns></returns>
        public bool EditInfo(CustomerInfo cust)
        {
            _infoDal.Update(cust);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<CustomerInfo> GetCustomerInfo(Guid id)
        {
            return _infoDal.LoadEntities(u => u.Customer_inf_id == id);
        }

        /// <summary>
        /// 用户信息分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IQueryable<CustomerInfo> LoadPage(PageParam page,out int total)
        {
            IQueryable<CustomerInfo> pageData;
            switch (page.Sort)
            {
                case nameof(CustomerInfo.Customer_name):
                    pageData = _infoDal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Customer_name == page.SearchText, u => u.Customer_name, page.Order);
                    break;
                case nameof(CustomerInfo.Customer_phone):
                    pageData = _infoDal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Customer_name == page.SearchText, u => u.Customer_phone, page.Order);
                    break;
                case nameof(CustomerInfo.Customer_email):
                    pageData = _infoDal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Customer_name == page.SearchText, u => u.Customer_email, page.Order);
                    break;
                case nameof(CustomerInfo.Birthday):
                    pageData = _infoDal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Customer_name == page.SearchText, u => u.Birthday, page.Order);
                    break;
                case nameof(CustomerInfo.Address):
                    pageData = _infoDal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Customer_name == page.SearchText, u => u.Address, page.Order);
                    break;
                default:
                    pageData = _infoDal.LoadPage(page.PageSize, page.PageIndex, out total, u => u.Customer_name == page.SearchText, u => u.Register_time, page.Order);
                    break;
            }            
            return pageData;

        }
    }
}
